-- NetworkStatus
-- Create by zouyb
-- 手机网络连接

module("NetworkStatus", package.seeall);

---------------------------------
-- 手机wifi是否连接
function isWifiConnected()
    local ret = false;
    if isAndroidPlatform() then
        ret = callJavaFunc("isWifiConnected", {}, "()Z");
    elseif isIphonePlatform() or isIpadPlatform() then
           ret = callOCFunc("isWifiConnected");
    else
        return true;
    end

    return ret;
end

---------------------------------
-- 手机移动网络是否连接
function isMobileConnected()
    local ret = false;
    if isAndroidPlatform() then
        ret = callJavaFunc("isMobileConnected", {}, "()Z");
    elseif isIosPlatform() then
        ret = callOCFunc("isMobileConnected");
    else
        return true;
    end
    return ret;
end

---------------------------------
-- 获取MAC地址
function getMacAddress()
    local ret = "";
    if isAndroidPlatform() then
        ret = callJavaFunc("getMacAddress", {}, "()Ljava/lang/String;");
        if not isValidMac(ret) then
            -- 如果不是合法的mac地址，返回空串
            return "";
        end
    elseif isIosPlatform() then
        return "";
    end

    return ret;
end

-- 获取ip地址
function getIPAddress()
    if isAndroidPlatform() then
        return callJavaFunc("getIPAddress", {}, "()Ljava/lang/String;");
    elseif isIosPlatform() then
        return callOCFuncWithClass("QCToolkit", "ipAddress");
    end
    return "";
end

-- 获取网线类型
function getNetMode()
    local ret = "";
    if isAndroidPlatform() then
        ret = callJavaFunc("getNetMode", {}, "()Ljava/lang/String;");
    elseif isIosPlatform() then
        ret = callOCFuncWithClass("QCToolkit", "getNetMode");
    end
    return ret;
end

-- 是否是合法的mac地址
function isValidMac(mac)
    if type(mac) ~= "string" or sizeof(mac) ~= 17 then
        return false;
    end

    local ret = mac:match("%x%x:%x%x:%x%x:%x%x:%x%x:%x%x");
    if type(ret) == "string"and sizeof(ret) == 17 then
        return true;
    end

    return false;
end
